#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScreen>
#include <QTest>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//默认动作跳转（点击消息体跳转）和按钮跳转 demo
void MainWindow::on_pushButton_3_clicked()
{
    QDBusInterface iface("org.freedesktop.Notifications",
                         "/org/freedesktop/Notifications",
                         "org.freedesktop.Notifications",
                         QDBusConnection::sessionBus());
    QList<QVariant> args;
    QStringList actions;  //跳转动作
    actions.append("kylin-recorder");
    actions.append("default");          //默认动作：点击消息体时打开麒麟录音
    actions.append("ukui-control-center");
    actions.append("控制面板");         //按钮动作：点击'控制面板'按钮时打开控制面板应用
    actions.append("kylin-music");
    actions.append("麒麟音乐");
    QMap<QString, QVariant> hints;
    args<<QString(tr("默认跳转和动作跳转demo"))         //应用名
        <<((unsigned int) 0)              //替换ID：若不替换则填0
        <<QString("icon")                 //应用主题图标名
        <<QString(tr("通知主题..."))    //通知主题
        <<QString(tr("通知正文..."))    //通知正文
        <<actions                      //跳转动作
        <<hints
        <<(int)0;                      //驻留时间： -1：默认显示时间（由通知中心决定） 0：消息常驻  其他：显示相应时长(由应用决定，单位ms)
    QDBusMessage response = iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
    if (response.type() == QDBusMessage::ReplyMessage)//判断method的返回值
    {
        qDebug() << "发送成功！通知ID：" << response.arguments().takeFirst().toString();
        m_notifyId = response.arguments().takeFirst().toUInt();  //返回的是通知中心为该条消息分配的ID
    }
    else{
        qDebug() << "发送失败：" << response.type();
    }
}

//链接跳转：例如截图应用，点击弹窗后会使用默认应用打开图片进行预览
void MainWindow::on_pushButton_5_clicked()
{
    QDBusInterface iface("org.freedesktop.Notifications",
                         "/org/freedesktop/Notifications",
                         "org.freedesktop.Notifications",
                         QDBusConnection::sessionBus());
    QList<QVariant> args;
    QStringList actions;
    QMap<QString, QVariant> hints;
    //链接跳转正文格式为：<a href="跳转链接">要显示的文本内容</a>
    QString bodyUrl = "/usr/share/icons/ukui-icon-theme-fashion/scalable/apps/mate-about.svg";
    QString bodyText = "显示的正文内容： mate-about.svg";
    QString bodyMsg = "<a href=\"" + bodyUrl + "\">" + bodyText + "</a>";
    args<<tr("链接跳转demo")
        <<(unsigned int)0
        <<QString("kylin-screenshot") //图标
        <<QString(tr("通知主题..."))    //通知主题
        <<bodyMsg   //正文
        <<actions
        <<hints
        <<(int)-1;
    QDBusMessage response = iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
    if (response.type() == QDBusMessage::ReplyMessage)  //判断method的返回值
    {
        qDebug() << "发送成功！通知ID：" << response.arguments().takeFirst().toString();
        m_notifyId = response.arguments().takeFirst().toUInt();
    }
    else{
        qDebug() << "发送失败：" << response.type();
    }
}

//关闭通知
void MainWindow::on_pushButton_4_clicked()
{
    QDBusInterface iface("org.freedesktop.Notifications",
                         "/org/freedesktop/Notifications",
                         "org.freedesktop.Notifications",
                         QDBusConnection::sessionBus());
    QList<QVariant> args;
    args<<m_notifyId; //m_notifyId为要关闭通知的ID号
    iface.callWithArgumentList(QDBus::AutoDetect,"CloseNotification",args);
    qDebug()<<"关闭通知 ID :"<<args.at(0).toUInt();
}

//替换通知
void MainWindow::on_pushButton_10_clicked()
{
    QDBusInterface iface("org.freedesktop.Notifications",
                         "/org/freedesktop/Notifications",
                         "org.freedesktop.Notifications",
                         QDBusConnection::sessionBus());
    QList<QVariant> args;
    QStringList actions;
    QMap<QString, QVariant> hints;
    m_replaceId = 1; //替换1号通知
    args<<tr("替换通知")
        <<m_replaceId         //替换ID
        <<QString("")
        <<QString(tr("新通知主题..."))
        <<QString(tr("新通知正文..."))
        <<actions
        <<hints
        <<(int)-1;
    QDBusMessage response = iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
    if (response.type() == QDBusMessage::ReplyMessage)//判断method的返回值
    {
        qDebug() << "发送成功！通知ID：" << response.arguments().takeFirst().toString();
        m_notifyId = response.arguments().takeFirst().toUInt();
    }
    else{
        qDebug() << "发送失败：" << response.type();
    }
}



